package com.smart.web;

import com.smart.dao.TeacherDao;
import com.smart.domain.Lesson;
import com.smart.domain.Teacher;
import com.smart.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.annotation.ModelAndViewResolver;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
public class Comtroller {

    public TeacherService teacherService;


    @Autowired
    public void setTeacherService(TeacherService teacherService) {
        this.teacherService = teacherService;
    }

    @RequestMapping(value = "/index.html")
    public String loginPage() {
        return "login";
    }

   /* @RequestMapping(value = "/loginCheck.html")
    public ModelAndView logincheck(HttpServletRequest request, LoginCommand loginCommand) {
        boolean isValidUser = teacherService.hasMatchUser(loginCommand.getUserNamecom(), loginCommand.getPasswordcom());
        if (!isValidUser) {
            return new ModelAndView("login", "error", "用户名或密码错误");

        }else {
            Teacher teacher = teacherService.queryteacher(loginCommand.getUserNamecom());
            request.getSession().setAttribute("teacher", teacher);

            return new ModelAndView("main");
        }

    }*/


    @RequestMapping("login")
    public ModelAndView login(String name,String pw) {
        List<Teacher> list=teacherService.getbyteachernameandps(name, pw);
        ModelAndView mad = new ModelAndView("main");
        mad.addObject("Teacher", list);
        return mad;
    }

    @RequestMapping("addlesson")
    public ModelAndView addlesson(Lesson lesson) {
        teacherService.addlesson(lesson);
        Lesson lesson1 =teacherService.querylesson();
        ModelAndView mad =new ModelAndView("home.jsp");
        mad.addObject("lessons",lesson1);
        return mad;


    }
}

